package cn.tedu.charging.user.controller;

import cn.tedu.charging.common.pojo.param.ChargeParam;
import cn.tedu.charging.common.pojo.param.VehicleBindParam;
import cn.tedu.charging.common.pojo.param.VehicleUnbindParam;
import cn.tedu.charging.common.pojo.vo.BalanceVO;
import cn.tedu.charging.common.pojo.vo.VehicleVO;
import cn.tedu.charging.common.protocol.JsonResult;
import cn.tedu.charging.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 定义车主服务相关的六个接口：
 * 登录
 * 查询余额
 * 充值
 * 查询车辆绑定
 * 绑定 解绑
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    // 小程序微信登录
    @GetMapping("/user/wx/login")
    public JsonResult<Integer> wxLogin(String nickName, String code) {
        // ToDo: 调用业务层，获取当前用户的userId（本地的，小程序车主用户）
        Integer userId = userService.wxLogin(nickName, code);
        return JsonResult.ok(userId);
    }

    @GetMapping("/user/balance/{userId}")
    public JsonResult<BalanceVO> getBalance(@PathVariable("userId") Integer userId) {
        // ToDo: 调用业务层，获取当前用户的余额
        BigDecimal balance = userService.getBalance(userId);
        BalanceVO vo = new BalanceVO();
        vo.setBalance(balance);
        return JsonResult.ok(vo);
    }

    @PostMapping("/user/charge")
    public JsonResult<Void> charge(@RequestBody ChargeParam param) {
        // ToDo: 调用业务层，充值
        userService.charge(param);
        return JsonResult.ok(null);
    }

    // 查询该用户正在绑定的车辆
    @GetMapping("/user/vehicle/binded/{userId}")
    public JsonResult<VehicleVO> bindVehicle(@PathVariable("userId") Integer userId) {
        VehicleVO vo = new VehicleVO();
        vo = userService.getUserBindedVehicle(userId);
        return JsonResult.ok(vo);
    }

    // 用户绑定车辆
    @PostMapping("/user/vehicle/bind")
    public JsonResult<Integer> bindVehicle(@RequestBody VehicleBindParam param) {
        Integer vehicleId = userService.bindVehicle(param);
        return JsonResult.ok(vehicleId);
    }

    // 用户解绑车辆
    @PostMapping("/user/vehicle/unbind")
    public JsonResult<Void> unbindVehicle(@RequestBody VehicleUnbindParam param) {
        userService.unbindVehicle(param);
        return JsonResult.ok(null);
    }
}
